﻿using System.Windows.Forms;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using Vintner.Items.ModuleData;
using Vintner.Masters.ModuleData;

namespace Vintner.Items.Scrounger
{
    public partial class TabItemCustomer : UserControl, iScroungerUserControl
    {
        private const string ItemCustomerSlave = "ITEMCUSTOMERSLAVE";
        private DRDescriptions dRCategory = null;
        private DRTaxCode dRTaxCode = null;
        private RRItemCategory rrItemCat = null;
        private DRItems dRItems = null;
        private DRItemsPrice dRItemsPrice = null;
        private ApplicationLibrary.DBManager.DBManager dbManager = null;

        public TabItemCustomer()
        {
            InitializeComponent();
        }

        public void OnAttachData(ApplicationLibrary.DBManager.DBManager dbManager)
        {
            this.dbManager = dbManager;
            dRCategory = new DRDescriptions(DescriptionType.E_Category);
            rrItemCat = new RRItemCategory();
            dRItems = new DRItems();
            dRItemsPrice = new DRItemsPrice();
            dRTaxCode = new DRTaxCode();

            dgwItemCustomer.AutoGenerateColumns = false;
            dgwItemCustomer.DataSource = dbManager.SlaveBinding(ItemCustomerSlave);
            dgwItemCustomer.AddReadOnlyColumn(VI_ItemCustomerPrice.Price, true);
            dgwItemCustomer.AddReadOnlyColumn(VI_ItemCustomerPrice.Description, true);
            dgwItemCustomer.AddReadOnlyColumn(VI_ItemCustomerPrice.TaxCode, true);

            //dgwItemCustomer.AddReadOnlyColumn(VI_ItemCustomerPrice.DescCategory, true);

            dgwItemCustomer.AddColumnChangeValue(VI_ItemCustomerPrice.Item, new ExtendedDataGridView.DataGridViewValueChangeEventHandler(ItemPrice_ItemChange));
            dgwItemCustomer.AddColumnValidator(VI_ItemCustomerPrice.Item, new ExtendedDataGridView.DataGridViewColumnValidatingEventHandler(ItemPrice_ItemValidating));
            dgwItemCustomer.AddColumnChangeValue(VI_ItemCustomerPrice.Category, new ExtendedDataGridView.DataGridViewValueChangeEventHandler(ItemPrice_CategoryChange));
            dgwItemCustomer.AddColumnValidator(VI_ItemCustomerPrice.Category, new ExtendedDataGridView.DataGridViewColumnValidatingEventHandler(ItemPrice_CategoryValidating));

            colItem.AttachCodeType("Items");
            colItem.RadarForm = new RadarItemsForm();
            colItem.DescriptionColumn = VI_ItemCustomerPrice.Description;

            colCategory.AttachDataReader(rrItemCat, VI_ItemsPrice.Category, VI_Descriptions.Description, false);

            //colCategory.DescriptionColumn = VI_ItemCustomerPrice.DescCategory;
            colCategory.AttachDescription(VI_ItemCustomerPrice.Price);
        }

        public void OnBindData(ApplicationLibrary.DBManager.iScrounger frm)
        {
            DocumentForm doc = (DocumentForm)frm;
            doc.BindColumn(colCustomer, VI_ItemCustomerPrice.Customer);
            doc.BindColumn(colItem, VI_ItemCustomerPrice.Item);
            doc.BindColumn(colDescr, VI_ItemCustomerPrice.Description);
            doc.BindColumn(colCategory, VI_ItemCustomerPrice.Category);

            //doc.BindColumn(colCatDesc, VI_ItemCustomerPrice.DescCategory);
            doc.BindColumn(colPrice, VI_ItemCustomerPrice.Price);
            doc.BindColumn(colTax, VI_ItemCustomerPrice.TaxCode);
            doc.BindControl(dgwItemCustomer);
        }

        private void ItemPrice_ItemChange(object sender, DataGridViewCellEventArgs e)
        {
            if (dbManager.Status != DBMode.Edit)
                return;
            string item = dgwItemCustomer.GetValue<string>(e.RowIndex, VI_ItemCustomerPrice.Item);
            if (!string.IsNullOrEmpty(item))
            {
                rrItemCat.Find(item);
                colCategory.RefreshDataReader();
                ItemPrice_CategoryChange(sender, e);
            }
        }

        private string ItemPrice_ItemValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            e.Cancel = false;
            return "";
        }

        private void ItemPrice_CategoryChange(object sender, DataGridViewCellEventArgs e)
        {
            string item = dgwItemCustomer.GetValue<string>(e.RowIndex, VI_ItemCustomerPrice.Item);
            string cat = dgwItemCustomer.GetValue<string>(e.RowIndex, VI_ItemCustomerPrice.Category);
            dRItemsPrice.Find(item, cat);
            dgwItemCustomer.SetValue<string>(e.RowIndex, VI_ItemCustomerPrice.Price, dRItemsPrice.GetValue<string>(VI_ItemsPrice.Price));
            dgwItemCustomer.SetValue<string>(e.RowIndex, VI_ItemCustomerPrice.TaxCode, dRItemsPrice.GetValue<string>(VI_ItemsPrice.TaxCode));
        }

        private string ItemPrice_CategoryValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            e.Cancel = false;
            return "";
        }

        private void dgwInvoice_DataGridViewDoubleClickCell(object sender, DataGridViewDoubleClickArgs e)
        {
            //if (e.ColumnName == PL_SalesInvoices.InvoiceNo.Name)
            //{
            //    Sales.Forms.SalesInvoice frm = (Sales.Forms.SalesInvoice)GlobalInfo.OpenForm("Plumber.Plumber.Sales.Forms.SalesInvoice", false);
            //    frm.FindRecord(new RadarSaleInvoicesParam(e.CellValue));
            //}
            //if (e.ColumnName == PL_SalesInvoices.Master.Name)
            //{
            //    Masters.Forms.Masters frm = (Masters.Forms.Masters)GlobalInfo.OpenForm("Plumber.Plumber.Masters.Forms.Masters", false);
            //    string type = dgwJE.GetValue<string>(e.Row, PL_SalesInvoices.Type);
            //    frm.FindRecord(new RadarMasterParam(type, e.CellValue));
            //}
        }

        public void AutoResizeColumns()
        {
            //dgwItemCustomer.AutoResizeColumns();
        }

        private void dgwItemCustomer_DataGridViewRowAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
        }

        private void dgwItemCustomer_DataGridViewRowEnter(object sender, DataGridViewCellEventArgs e)
        {
            string item = dgwItemCustomer.GetValue<string>(e.RowIndex, VI_ItemCustomerPrice.Item);
            if (item != null)
            {
                rrItemCat.Find(item);
                colCategory.RefreshDataReader();
            }
        }

        public void OnPrepareAuxData()
        {
            foreach (DataGridViewRow dr in dgwItemCustomer.Rows)
            {
                string item = DataGridViewRowValues.GetValue<string>(dr, VI_ItemCustomerPrice.Item);
                if (!string.IsNullOrEmpty(item))
                {
                    dRItems.Find(item);
                    DataGridViewRowValues.SetValue<string>(dr, VI_ItemCustomerPrice.Description, dRItems.GetValue<string>(VI_Items.Description));
                }
                string cat = DataGridViewRowValues.GetValue<string>(dr, VI_ItemCustomerPrice.Category);

                //if (!string.IsNullOrEmpty(cat))
                //{
                //    dRCategory.Find(cat);
                //    DataGridViewRowValues.SetValue<string>(dr, VI_ItemCustomerPrice.DescCategory, dRCategory.GetValue<string>(VI_Descriptions.Description));
                //}

                if (!string.IsNullOrEmpty(item) && !string.IsNullOrEmpty(cat))
                {
                    dRItemsPrice.Find(item, cat);
                    DataGridViewRowValues.SetValue<string>(dr, VI_ItemCustomerPrice.Price, dRItemsPrice.GetValue<string>(VI_ItemsPrice.Price));
                    string tax = dRItemsPrice.GetValue<string>(VI_ItemsPrice.TaxCode);
                    if (!string.IsNullOrEmpty(tax))
                    {
                        dRTaxCode.Find(tax);
                        DataGridViewRowValues.SetValue<string>(dr, VI_ItemCustomerPrice.TaxCode, dRTaxCode.GetValue<string>(VI_TaxCode.Description));
                    }
                }
            }
        }
    }
}